﻿Imports System.IO
Public Class frmNewItem
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Dim On_LoadGroup As Boolean = False
    Dim On_LoadClass As Boolean = False
    Dim DT_ItemGroup As New DataTable
#Region "Function"
    Sub Load_BudgetYear()

        Try
            Strsql = "select min(budget_year) as year from items group by budget_year limit 1"
            Dim MinYear As Integer
            MinYear = _mysql.MySQLExecuteScalar(Strsql)

            If MinYear = 0 Then
                Me.CboBudgetYear.Properties.Items.Add(Now.Year)
            Else
                For i As Integer = (Now.Year + 543) To MinYear Step -1
                    Me.CboBudgetYear.Properties.Items.Add(i)
                    i = +i
                Next
            End If
            Me.CboBudgetYear.Text = (Now.Year + 543)
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_BudgetYear"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Budget()
        Try
            Strsql = "Select Budget_ID,Budget_Name from budget"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "budget")
            With Me.CboBudgetType
                .DataSource = DT_Budget
                .DisplayMember = "Budget_Name"
                .ValueMember = "Budget_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Budget"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_Department()
        Try
            Strsql = "Select Department_ID,Department_Name from department  order by Department_Name"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "department")
            With Me.CboDepartment
                .DataSource = DT_Budget
                .DisplayMember = "Department_Name"
                .ValueMember = "Department_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_LastID()
        Try
            Strsql = "Select Item_Code from items where item_type = '" & CboClass.SelectedValue.ToString.Trim & "' order by Item_Code desc"
            Dim MyItemClass As String
            Dim DT_Items As New DataTable
            DT_Items = _mysql.GetMYSQLDataTable(Strsql, "items")
            If DT_Items.Rows.Count = 0 Then
                Me.lblLastID.Text = " ไม่พบ"
            Else
                Me.lblLastID.Text = DT_Items.Rows(0).Item(0).ToString
            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_LastID"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Insert_Data()
        Try
            Strsql = "Insert into items (item_type,Item_Code,Item_name,Item_Desc,Item_Price,Item_Budget,Budget_year,Receive_Date,Department,Ref_DocReceiveID,Receiveby,Band,"
            Strsql = Strsql & " Item_EngName,Product_From_Country,Item_Model,Item_From_Company,serialno,no_receive_date,item_image_path,Item_Status,usagetime,purchasetype,wait_code,item_non_depreciation,Ref_DocDrawID,Draw_Date) "
            Strsql = Strsql & " values ('" & CboClass.SelectedValue.ToString.Trim & "',"
            If Me.ChkWaitCode.Checked = True Then
                Strsql = Strsql & "''"
            Else
                Strsql = Strsql & "'" & Me.TxtItemCode.Text & "'"
            End If

            Strsql = Strsql & ",'" & Me.TxtItemName.Text & "','" & Me.TxtDesc.Text & "'," & Replace(TxtPrice.Text, ",", "") & ","
            Strsql = Strsql & Me.CboBudgetType.SelectedValue & ",'" & CboBudgetYear.Text & "'"

            Select Case Me.ChkNonReceiveDate.Checked
                Case True
                    Strsql = Strsql & ",NULL"
                Case False
                    Strsql = Strsql & ",'" & Pn_Framework.MysqlDateTimeFormat(Me.DtpRecDate.DateTime, False) & "'"
            End Select
            Strsql = Strsql & "," & Me.CboDepartment.SelectedValue & ",'" & Me.TxtRecDocID.Text & "','" & Me.TxtReceiveUser.Text & "','" & Me.TxtBand.Text & "','" & Me.TxtEngName.Text & "'"
            Strsql = Strsql & ",'" & Me.CboCountry.SelectedValue & "','" & Me.TxtModel.Text & "','" & Me.TxtCompany.Text & "','" & Me.TxtSerialNo.Text & "','" & IIf(Me.ChkNonReceiveDate.Checked = True, "Y", "N") & "'"


            If Me.PictureBox1.Image Is Nothing Then
                Strsql = Strsql & ",''"

            Else
                If Me.TxtImgpart.Text <> "" Then
                    Copy_Image2Server()
                    Strsql = Strsql & ",'" & Replace(Me.TxtImgpart.Text, "\", "\\") & "'"
                Else
                    Strsql = Strsql & ",''"
                End If
              
            End If

            Strsql = Strsql & ",0," & Me.NudAge.Value & "," & Me.Cbopurchasetype.SelectedValue & ",'" & IIf(Me.ChkWaitCode.Checked = True, "Y", "N") & "'"
            Strsql = Strsql & ",'" & IIf(Me.Chkitem_non_depreciation.Checked = True, "Y", "N") & "','" & Me.TxtDrawDocID.Text & "'"
            Select Case Me.ChkNonDrawDate.Checked
                Case True
                    Strsql = Strsql & ", Null"
                Case False
                    Strsql = Strsql & ",'" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDrawDate.DateTime, False) & "'"

            End Select
            Strsql = Strsql & ")"

            If _mysql.MySQLExecute(Strsql) = -1 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "Insert_Data"
                    .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            Else
                MsgBox("เพิ่มข้อมูลเข้าในระบบเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                Me.Dispose()
            End If

            'Get Last Insert Item_ID
            Strsql = "SELECT LAST_INSERT_ID();"
            Dim Last_ITEM_ID As Integer = 0
            Last_ITEM_ID = _mysql.MySQLExecuteScalar(Strsql)


            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (1,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Last_ITEM_ID & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Insert_Data"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Insert_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Me.CboClass.SelectedValue.ToString.Trim & "-" & Replace(Me.TxtItemCode.Text, "/", "") & " " & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImgpart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImgpart.Text)
            PictureBox2.Image = bm
            Me.TxtImgpart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = StrFilename
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Purchasetype()
        Try
            Strsql = "select purchase_type_id,purchase_type_name from purchasetype"
            Dim DT_PurchaseType As New DataTable

            DT_PurchaseType = _mysql.GetMYSQLDataTable(Strsql, "purchasetype")
            With Cbopurchasetype
                .DataSource = DT_PurchaseType
                .DisplayMember = "purchase_type_name"
                .ValueMember = "purchase_type_id"


            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Purchasetype"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Validate_Code() As Boolean
        Validate_Code = False
        Try

            Dim ItemCode As String = ""
            ItemCode = Replace(Me.TxtItemCode.Text, "_", "")


            If ItemCode.Length < 4 Then
                PictureBox1.Image = WHAS.My.Resources.Resources.delete
                lblDuplicateStatus.Text = "คุณไม่สามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Red
                Me.BtnSave.Enabled = False
                Exit Function
            End If

            Strsql = "select count(*) from items where  item_type = '" & Me.CboClass.SelectedValue & "' and Item_Code = '" & Me.TxtItemCode.Text & "'"
            Dim DuplicateItem As Integer = 0
            DuplicateItem = _mysql.MySQLExecuteScalar(Strsql)
            If DuplicateItem = 0 Then
                PictureBox1.Image = WHAS.My.Resources.Resources.check
                lblDuplicateStatus.Text = "คุณสามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Green
                Me.BtnSave.Enabled = True
                Validate_Code = True
            Else
                PictureBox1.Image = WHAS.My.Resources.Resources.delete
                lblDuplicateStatus.Text = "คุณไม่สามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Red
                BtnSave.Enabled = False
                '(MsgBox("ไม่สามารถใช้หมายเลขคุรุภัณฑ์นี้ได้", MsgBoxStyle.Exclamation, "คำเตือน"))
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Function
    'Sub Load_Discharge_Reson()
    '    Try
    '        Strsql = "Select discharge_reson_id,discharge_reson_name from discharge_reson"
    '        Dim DT_Budget As New DataTable
    '        DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "budget")
    '        With Me.CboDischargeStatus
    '            .DataSource = DT_Budget
    '            .DisplayMember = "discharge_reson_name"
    '            .ValueMember = "discharge_reson_id"
    '        End With
    '    Catch ex As Exception
    '        With frmDebug
    '            .lblFormName.Text = Me.Name
    '            .lblFunctionName.Text = "Load_Discharge_Reson"
    '            .MemoErr_Description.Text = ex.Message
    '            .MemoSQL.Text = Strsql
    '            .ShowDialog()
    '        End With
    '    End Try
    'End Sub
    Function Lock_Control(ByVal Enable As Boolean)
        Try

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Sub Load_Nationality()
        Try
            Strsql = "select * from nationality order by nationality_ID"
            Dim DT_Nationality As DataTable
            DT_Nationality = _mysql.GetMYSQLDataTable(Strsql, "Nationality")
            With Me.CboCountry
                .DataSource = DT_Nationality
                .DisplayMember = "nationality_Name"
                .ValueMember = "nationality_ID"
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Nationality"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_ItemClass()
        Try
            Dim DT_Itemclass As New DataTable
            Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem  where type_id like '" & Me.CboGroup.SelectedValue.ToString.Trim & "%'"
            DT_Itemclass = _mysql.GetMYSQLDataTable(Strsql, "typeitem")
            With Me.CboClass
                .DataSource = DT_Itemclass
                .DisplayMember = "fullclassname"
                .ValueMember = "Type_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemClass"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_ItemGroup()
        Try
            Dim DT_ItemCate As New DataTable
            Strsql = "Select * from categories"
            DT_ItemCate = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_ItemCate
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemGroup"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

        Try
            Dim DT_ItemGroup As New DataTable
            'Strsql = "Select Section_ID,Section_Name from sections order by section_id"
            Strsql = "select section_id,section_name  from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "' order by section_id "
            DT_ItemGroup = _mysql.GetMYSQLDataTable(Strsql, "sections")
            With Me.CboGroup
                .DataSource = DT_ItemGroup
                .DisplayMember = "section_name"
                .ValueMember = "Section_ID"

            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemGroup"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
        Me.CboClass.Text = ""
        Try

            Dim DT_Itemclass As New DataTable
            Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem where type_id like '" & Me.CboGroup.SelectedValue.ToString.Trim & "%'"
            DT_Itemclass = _mysql.GetMYSQLDataTable(Strsql, "typeitem")
            With Me.CboClass
                .DataSource = DT_Itemclass
                .DisplayMember = "fullclassname"
                .ValueMember = "Type_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemGroup"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_ItemStatus()
        Try
            Strsql = "Select Status_ID,Status_Name from itemstatus"
            Dim DT_ItemStatus As New DataTable
            DT_ItemStatus = _mysql.GetMYSQLDataTable(Strsql, "itemstatus")
            With Me.CboItemStatus
                .DataSource = DT_ItemStatus
                .DisplayMember = "Status_Name"
                .ValueMember = "Status_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemStatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Function Validate_Data() As Boolean
        Try
            Validate_Data = False
            Try
                If Me.ChkWaitCode.Checked = False Then
                    If Me.TxtItemCode.Text = "" Then
                        MsgBox("กรุณาใส่รหัสครุภัณฑ์", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                        Exit Function
                    End If
                End If

                If Me.TxtItemName.Text = "" Then
                    MsgBox("กรุณาใส่ชื่อครุภัณฑ์", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                    Exit Function
                End If
                Validate_Data = True
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Load_AutoComplete_Item_Name()
        Try
            Strsql = "select Item_Name from items WHERE items.Item_Name IS NOT NULL  AND items.Item_Name <>  '' AND items.Item_Type =  '" & Me.CboClass.SelectedValue & "' group by Item_Name"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_Name"))
            Next
            Me.TxtItemName.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Name"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_Band()
        Try
            Strsql = "select Band from items where Band is not null and Band <> '' group by Band"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Band"))
            Next
            Me.TxtBand.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Band"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_EngName()
        Try
            Strsql = "select Item_EngName from items where Item_EngName is not null and  Item_EngName <> '' group by Item_EngName"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_EngName"))
            Next
            Me.TxtEngName.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_EngName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_Model()
        Try
            Strsql = "select Item_Model from items where Item_Model is not null and  Item_Model <> ''   group by Item_Model"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_Model"))
            Next
            Me.TxtModel.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Model"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_From_Company()
        Try
            Strsql = "select Item_From_Company from items where Item_From_Company is not null and Item_From_Company <> '' group by Item_From_Company"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_From_Company"))
            Next
            Me.TxtCompany.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_From_Company"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Receivername()
        Try
            Strsql = "select receiveby from items where receiveby is not null and receiveby <> '' group by receiveby"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("receiveby"))
            Next
            Me.TxtReceiveUser.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Receivername"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
  
#End Region


    Private Sub frmNewItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Budget()
        Load_Department()

        Load_BudgetYear()
        Load_Nationality()
        Load_ItemGroup()
        Load_ItemStatus()
        Load_LastID()
        'Load_Discharge_Reson()
        Load_Purchasetype()
        'Load Autocomplete
        Load_AutoComplete_Item_Name()
        Load_AutoComplete_Item_Band()
        Load_AutoComplete_Item_EngName()
        Load_AutoComplete_Item_Model()
        Load_AutoComplete_Item_From_Company()
        Load_AutoComplete_Receivername()
        On_Load = False

    End Sub

    Private Sub TxtItemCode_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtItemCode.EditValueChanged
        If Me.TxtItemCode.Text = "" Then Exit Sub
        Validate_Code()
    End Sub

    Private Sub TxtItemCode_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtItemCode.LostFocus
        If Me.TxtItemCode.Text = "" Then Exit Sub
        Validate_Code()
    End Sub
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub


    Private Sub CboGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboGroup.SelectedIndexChanged
        Try
            If On_Load = True Then Exit Sub
            If On_LoadGroup = True Then Exit Sub
            On_LoadClass = True
            Me.CboClass.Text = ""
            Dim SectionCode As String = ""
            SectionCode = Me.CboGroup.SelectedIndex
            If DT_ItemGroup.Rows(Me.CboGroup.SelectedIndex).Item("Cate_Code").ToString <> "" Then
                SectionCode = DT_ItemGroup.Rows(Me.CboGroup.SelectedIndex).Item("Cate_Code").ToString
                If DT_ItemGroup.Rows(Me.CboGroup.SelectedIndex).Item("Section_Code").ToString <> "" Then
                    SectionCode = SectionCode & "-" & DT_ItemGroup.Rows(Me.CboGroup.SelectedIndex).Item("Section_Code").ToString
                End If
            Else
                SectionCode = DT_ItemGroup.Rows(Me.CboGroup.SelectedIndex).Item("section_id").ToString
            End If

            Dim DT_Itemclass As New DataTable
            Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem  "
            If Len(SectionCode) > 4 Then
                Strsql = Strsql & " where left(type_id,8) = '" & SectionCode & "'"
            Else
                Strsql = Strsql & " where left(type_id,4) = '" & SectionCode & "'"
            End If

            DT_Itemclass = _mysql.GetMYSQLDataTable(Strsql, "typeitem")
            With Me.CboClass
                .DataSource = DT_Itemclass
                .DisplayMember = "fullclassname"
                .ValueMember = "Type_ID"
            End With
            On_LoadClass = False
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "CboGroup_SelectedIndexChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Private Sub CboClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboClass.SelectedIndexChanged
        If On_Load = True Then Exit Sub
        If On_LoadGroup = True Then Exit Sub
        If On_LoadClass = True Then Exit Sub

        Load_AutoComplete_Item_Name()

        Load_LastID()
    End Sub

    Private Sub PicLoadImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicLoadImg.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImgpart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImgpart.Text)
                PictureBox2.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Frmcapture.StrItemName = Me.CboClass.SelectedValue & "-" & Me.TxtItemCode.Text
        Frmcapture.ShowDialog()
        If Frmcapture.ImagePart = "" Then Exit Sub
        'Me.TxtImgpart.Text = Frmcapture.MyCapturepart
        Me.TxtImgpart.Text = Frmcapture.sfdImage.FileName
        Dim bm As New Bitmap(Me.TxtImgpart.Text)
        PictureBox2.Image = bm
        Frmcapture.Dispose()
    End Sub

    Private Sub picZoomin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picZoomin.Click
        FrmFullSize.PictureEdit1.Image = Me.PictureBox1.Image
        FrmFullSize.Show()
    End Sub

    Private Sub picClearimg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picClearimg.Click
        Try
            PictureBox1.Image = Nothing
            Me.TxtImgpart.Text = ""
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            'If Me.ChkWaitCode.Checked = True Or Me.ChkPassadu.Checked = True Then
            'Else
            If Validate_Data() = False Then Exit Sub
            ' End If

            If (MsgBox("คุณต้องการเพิ่มข้อมูลรหัสครุภัณฑ์ " & CboClass.SelectedValue.ToString.Trim & Me.TxtItemCode.Text & " ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการเพิ่มข้อมูล")) = MsgBoxResult.No Then Exit Sub
            Insert_Data()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub ChkWaitCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkWaitCode.CheckedChanged

        If ChkWaitCode.Checked = True Then
            Me.TxtItemCode.Text = ""
            Me.lblDuplicateStatus.Visible = False
            Me.PictureBox1.Visible = False
            Me.TxtItemCode.Enabled = False
            Me.BtnSave.Enabled = True
        Else
            Me.lblDuplicateStatus.Visible = True
            Me.PictureBox1.Visible = True
            Me.TxtItemCode.Enabled = True
        End If



    End Sub

   

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        If On_Load = True Then Exit Sub
        On_LoadGroup = True

        Try

            'Strsql = "Select Section_ID,Section_Name from sections order by section_id"
            Strsql = "select section_id,section_name,ifnull(Cate_Code,'') as Cate_Code,ifnull(Section_Code,'') as Section_Code   from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "' order by section_id "
            DT_ItemGroup = _mysql.GetMYSQLDataTable(Strsql, "sections")
            With Me.CboGroup
                .DataSource = DT_ItemGroup
                .DisplayMember = "section_name"
                .ValueMember = "Section_ID"

            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "CboCategories_SelectedIndexChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
        Me.CboClass.Text = ""
        Try
            Dim SectionCode As String = ""
            If DT_ItemGroup.Rows(0).Item("Cate_Code").ToString <> "" Then
                SectionCode = DT_ItemGroup.Rows(0).Item("Cate_Code").ToString
            Else
                SectionCode = DT_ItemGroup.Rows(0).Item("section_id").ToString
            End If
            Dim DT_Itemclass As New DataTable
            Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem where left(type_id,4) = '" & SectionCode & "'"
            DT_Itemclass = _mysql.GetMYSQLDataTable(Strsql, "typeitem")
            With Me.CboClass
                .DataSource = DT_Itemclass
                .DisplayMember = "fullclassname"
                .ValueMember = "Type_ID"
            End With
            If DT_Itemclass.Rows.Count = 0 Then
                CboClass.DataSource = Nothing
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "CboCategories_SelectedIndexChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
           
        End Try
        On_LoadGroup = False
    End Sub
End Class